.PHONY: test integration-test

majorVersion 	:= 1
minorVersion 	:= 0
patchVersion 	:= 16
buildVersion 	?= $(shell date -u +"%Y-%m-%dT%H-%M-%SZ")

version := $(majorVersion).$(minorVersion).$(patchVersion).$(buildVersion)

registry 	?=
image 		:= v8s-api
tag 		:= $(registry)/$(image):$(version)

buildDir ?= ./target

clean:
	$(info [$(CURDIR)] cleaning "$(buildDir)")
	@rm -rdf $(buildDir)

generate-sources: $(if $(workspaceMode),,clean)
	@echo "[$(CURDIR)] nothing to generate"

build: $(if $(workspaceMode),,generate-sources)
	$(info [$(CURDIR)] building "$(version)")
	CGO_ENABLED=0 GOOS=linux go build -ldflags="-X 'main.version=$(version)'" -o $(buildDir)/$(image) ./cmd

test:
	$(info [$(CURDIR)] running tests)
	@gotestsum --junitfile $(buildDir)/unit-tests.xml -- ./...

integration-test:
	$(info [$(CURDIR)] running integration-tests)
	@gotestsum --junitfile $(buildDir)/unit-tests.xml -- ./... --tags=integration

docker-release:
	$(info [$(CURDIR)] releasing "$(tag)")
	@docker build .  -t $(tag) --no-cache --build-arg binaryPath=$(buildDir)/$(image) --build-arg binaryVersion=$(version)
	@docker push $(tag)
	@echo '$(tag)' > $(buildDir)/release-version.txt

release: $(if $(workspaceMode),,build) docker-release